<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<% String path = request.getContextPath() ;%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %> 
<%@ taglib uri="/WEB-INF/tld/fmt.tld" prefix="fmt" %>
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>新增停车场信息</title>
<jsp:include page="../../import.jsp" flush="true" />
<jsp:include page="assParkCommon.jsp" flush="true" /><!-- 时间格式转换 -->
</head>
<body class="iframeBody">
	<div class="Wrapper">
		<div>
			<form id="assParkFormSet">
				<div class="fieldset1 tabel_col clearFix">
					<div class="caption">
						<span class="title">新增停车场信息</span>
					</div>
					<div class="fieldBox col_33">
						<div class="label">停车场编号</div>
						<div class="field">
							<input name="parkCd" id="parkCd" value="" type="text" disabled placeholder="自动生成" class="ui-textbox">
							<span class="requiredSymbol">*</span>
						</div>
					</div>
					<div class="fieldBox col_33">
						<div class="label">停车场名称</div>
						<div class="field">
							<input name="parkNm" id="parkNm" value="" type="text" class="ui-textbox" validate="{required:true}">
							<span class="requiredSymbol">*</span>
						</div>
					</div>
					<div class="fieldBox col_33">
						<div class="label">停车场联系人</div>
						<div class="field">
							<input name="contactNm" id="contactNm" type="text" class="ui-textbox" value="" validate="{required:true}">
							<span class="requiredSymbol">*</span>
						</div>
					</div>
					<div class="fieldBox col_33">
						<div class="label">停车场联系人电话</div>
						<div class="field">
							<input name="contactTel" id="contactTel" type="text" class="ui-textbox" value="" validate="{required:true, mobile:true}">
							<span class="requiredSymbol">*</span>
						</div>
					</div>
					<div class="fieldBox col_66_auto">
						<div class="label">停车场城市</div>
						<div class="field">
	                    	<jsp:include page="../../system/sysRegion/region.jsp" flush="true" >
								<jsp:param name="regionShowLvl" value="2"/>
							</jsp:include>
	                    </div>
					</div>
					<div class="fieldBox col_100_auto">
						<div class="label">合同有效期</div>
						<div class="field">
							<input name="beginDt" id="beginDt" type="text" class="ui-datepicker" value="" validate="{required:true}">
							<span>---</span>
							<input name="endDt" id="endDt" type="text" class="ui-datepicker" value="" validate="{required:true}">
							<span class="requiredSymbol">*</span>
						</div>
					</div>
					<div class="fieldBox col_100">
						<div class="label">停车场地址</div>
						<div class="field">
	                    	<input name="parkAddress" id="parkAddress" type="text" class="ui-textbox" value="" validate="{required:true}">
	                    	<span class="requiredSymbol">*</span>
	                    </div>
					</div>
					<div class="fieldBox col_100">
						<div class="label">资产管理员</div>
						<div class="field">
							<c:forEach items="${assMgsList}" var="assMgs">
	                            <a href="javascript:void(0);" id="A${assMgs.id}" onclick="delNameCode(${assMgs.id})" >${assMgs.managerRlNm}[${assMgs.managerNm}]</a>&nbsp;&nbsp;
							</c:forEach>
							<span id="namecodeflag" />
						</div>
					</div>
					<div id="addAssMg" class="fieldBox col_100_auto" style="display:none;">
						<div class="label">添加资管员</div>
						<div class="field">
							<input name="usrNmSearch" id="usrNmSearch" type="text" class="ui-textbox" value="">
							<font color="red">提示：输入工号添加资产管理员！</font>
						</div>
					</div>
					<div class="fieldBox col_100">
						<div class="label">附件上传</div>
						<div class="field">
							<jsp:include page="../assCommFilesUpload.jsp" flush="true" >
								<jsp:param name="filePath" value="ass"/>
							</jsp:include>
							<div id="fileUpload1" class="upfileBoxs"></div>
							<span class="sq-button sq-button-hasicon" id="btnUpload1" onclick="ajaxFileUploadImg('uploads1',1)">
								<i class="sqBtnIco_9"></i>上传
							</span>
						</div>
					</div>
					<div class="fieldBox col_100">
						<div style="width: 98%; margin-left: 1%;">
							<jsp:include page="../assCommFilesList.jsp" flush="true" ></jsp:include>
							<div id="ligergrid_file1" class="sq-grid"></div>
						</div>
					</div>
					<!-- 隐藏主键、系统代码(融资租赁系统)、用户状态(启用)、有效期ID -->
	                <div class="fieldBox col_100" style="display:none;">
	                    <div class="field">
	                         <input name="pId" id="pId" type="text" class="ui-textbox" value="${id}">
	                         <input name="searchSysCd" id="searchSysCd" type="text" class="ui-textbox" value="11000">
	                         <input name="isValid" id="isValid" type="text" class="ui-textbox" value="1">
	                         <input name="validId" id="validId" type="text" class="ui-textbox" value="${validId}">
	                         <!-- 上传文件使用：系统时间和当前登录人姓名 -->
	                         <input name="sysDate" id="sysDate" type="text" class="ui-textbox" value="${sysDate }">
	                         <input name="loginUsrNm" id="loginUsrNm" type="text" class="ui-textbox" value="${loginUsrNm }">
	                    </div>
	                </div>
				</div>
				
				<div class="btnsBox">
					<span id="saveAssParkingInfoBtn" class="sq-button sq-button-round" onclick="saveAssParkingInfo()">保存</span>
					<span id="closeBtn" class="sq-button sq-button-round" onclick="Remove_navTab()">关闭</span>
				</div>
			</form>
		</div>
	</div>
	<script type="text/javascript">
		//加载	
		$(function() {
	        initData();//初始化数据
	        getUsrList();//加载搜索用户方法
	    	loadFilesLiger("tccfj", 1, $("#validId").val());//加载停车场文件列表信息
	    });
		//初始化数据
	    function initData(){
	    	initForm();//初始化表单
	    	initFormData();//初始化信息
	    }
		//初始化表单
	    function initForm(){
	    	$("#assParkFormSet").ligerForm({
				validate: true
			});
			$("#assParkFormSet").check_ligerFormEditor();//重新刷新页面动态字段
		};
		
		//修改-初始化信息
		function initFormData(){
			var id = $("#pId").val();
			if(id){//停车场ID不为空执行
		    	var _url = '<%=path%>/ass/assParking/getById.do';
		    	$.ajax({
		    		type: 'get',
		    		dataType: 'json',
		    		data: {id:id},
		    		url: _url,
		    		success:function(msg){
		    			 if(msg.success == 1){
		    				 setData(msg.data);//设置数据
		    			 }else{
		    				 $.ligerDialog.SQalert({content:msg.info, SQ_type: "error"});
		    			 }
		    		}
		    	});
		    	//显示添加资产管理员部分
		    	$("#addAssMg").attr('style','display:block');
			}
    	}
		
		//设置固定字段数据	
		function setData(data){
			var form = liger.get("assParkFormSet");
			form.setData({
				parkCd: data.parkCd,
				parkNm: data.parkNm,
				parkAddress: data.parkAddress,
				contactNm: data.contactNm,
				contactTel: data.contactTel,
				validId: data.validId,
				beginDt: renderDate1(data.beginDt),
				endDt: renderDate1(data.endDt)
	    	});
			//初始化区域信息
			setRgnProvCdUpdataValue(data.provinceCd);
			setRgnCityCdUpdataValue(data.cityCd);
			initRegionProvSelectData();
			//加载日期插件
			$("#beginDt").trigger("change");
			$("#endDt").trigger("change");
		}
		
		//保存停车场相关信息
		function saveAssParkingInfo(){
			var ligerForm = liger.get("assParkFormSet");
			if (!ligerForm.valid()) {
				$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_MUST_FIX_ITEM_INFO,SQ_type: "warning"});
				return false; //验证不通过，不执行以下代码
			}
			//判断城市是否为空
			if(!getRgnProvCd()){
				$.ligerDialog.SQalert({content: "请选择所在省信息！",SQ_type: "warning"});
				return false;
			}
			if(!getRgnCityCd()){
				$.ligerDialog.SQalert({content: "请选择所在市信息！",SQ_type: "warning"});
				return false;
			}
			
			//判断合同有效期
			var beginDt = $("#beginDt").val();
			var endDt = $("#endDt").val();
			if(beginDt > endDt){
				$.ligerDialog.SQalert({content: "输入的合同有效期-终止日期应大于起始日期！",SQ_type: "warning"});
				return false;
			}
			
			//获取停车场ID-为空执行添加,否则修改
			var pId = $("#pId").val();
			var data = ligerForm.getData();//获取表单数据
			data["id"] = pId;
			data["provinceCd"] = getRgnProvCd();
			data["cityCd"] = getRgnCityCd();
			//默认新增停车场信息
			var url = '<%=path%>/ass/assParking/add.do';
			if(pId){//为空执行修改
				url = '<%=path%>/ass/assParking/modify.do';
			}
			var wait_dialog = null;
			$.ajax({
				type: 'post',
				dataType: 'json',
				data: data,
				url: url,
				beforeSend: function(){
					wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING ,SQ_type: "waitting" });
				},
				success:function(msg){
					wait_dialog.close();
					if(msg.success == 1){
						if(null != msg.data){
							$("#pId").val(msg.data);
						}
						saveVaildInfo();//修改停车场有效期信息
					}else{
						$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
					}
				}
			});
		}
		
		//保存有效期信息
		function saveVaildInfo(){
			var ligerForm = liger.get("assParkFormSet");
			var data = ligerForm.getData();
			var vId = $("#validId").val();//有效期ID
			//默认新增停车场有效期信息
			var url = '<%=path%>/ass/assParkingValid/add.do';
			if(vId){//为空执行修改
				url = '<%=path%>/ass/assParkingValid/modify.do';
			}
			data["validId"] = vId;
			data["parkId"] = $("#pId").val();;
			$.ajax({
				type: 'post',
				dataType: 'json',
				data: data,
				url: url,
				beforeSend: function(){
					//wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING ,SQ_type: "waitting"});
				},
				success:function(msg){
					//wait_dialog.close();
					//返回的停车场有效期ID不为空成功
					if(msg.success == 1){
						if(null != msg.data){
							$("#validId").val(msg.data);
						}
						saveFile();//保存附件信息
						$("#addAssMg").attr('style','display:block');//显示添加资产管理员部分
						$.ligerDialog.SQalert({content: msg.info,SQ_type: "success"});
					}else{
						$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
					}
				}
			});
		}
		
		//删除选中的资产管理员
		function delNameCode(mId){
			$.ligerDialog.confirm('确定删除此资管员吗?', function (flag) {
				if(flag == false)return;
				url = '<%=path%>/ass/assParkingMg/removeById.do';
				$.ajax({
					type: 'post',
					dataType: 'json',
					data: {id:mId},
					url: url,
					beforeSend: function(){
						wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING ,SQ_type: "waitting" });
					},
					success:function(msg){
						wait_dialog.close();
						if(msg.success == 1){
							$("#A"+mId).remove();//移除页面该元素
							top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "success"});
						}else{
							top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
						}
					}
				});
			});
		}
		
		//获取优车资产管理员
		function getUsrList(){
			var parms={
				"isValid": $("#isValid").val(),
				"searchSysCd": $("#searchSysCd").val(),
				"usrNmSearch": ""
			};
			$.ajax({
				url: "<%=path%>/ass/assParking/searchManagerList.do",
	    		type: "get",
	    		dataType: "json",
	    		data: parms,
	    		success : function(data) {
	    			$("#usrNmSearch").autocomplete(data, {
	    				minChars : 1,
	    				max : 30,
	    				mustMatch : false,	//如果不匹配则清空-需要设置为true
						matchCase:false,
	    				matchContains : true,
	    				formatItem : function(row, i, max) {//展示
	    					return row.managerRlNm+"["+row.managerNm+"]";
	    				},
	    				formatMatch : function(row, i, max) {//匹配
							return row.managerNm;
	    				},
	    				formatResult : function(row) {//返回
	    					return row.managerNm;
	    				}
	    			});
	    			//当选择一行结果后继续要执行的操作
	    		 	$("#usrNmSearch").result(function(event, data, formatted){
						if(data!=null){
							var parkId = $("#pId").val();
							//如果停车ID为空-不允许添加资管员
							if(!parkId){
								top.$.ligerDialog.SQalert({content: "请先保存基本信息！",SQ_type: "error"});
								$('#usrNmSearch').val("");
								return false;
							}else{
								var param={
									"parkId": parkId,
									"managerId": data.managerId,
									"managerNm": data.managerNm
								};
								jQuery.ajax({
									url:"<%=path%>/ass/assParkingMg/add.do",
									type:"post",
									dataType:"json",
						    		data: param,
									success:function (backdata){
										if(backdata.success == 1){
											top.$.ligerDialog.SQalert({content: "添加成功！",SQ_type: "success"});
											var trStr = '<a href="javascript:void(0);" id="A'+backdata.data+'" onclick="delNameCode('+backdata.data+')" >';
											trStr += data.managerRlNm + '[' + data.managerNm + ']</a>&nbsp;&nbsp;';
											$("#namecodeflag").before(trStr);
										}else if(backdata.success == 0){
											$.ligerDialog.SQalert({content: "此资产管理员已存在！", SQ_type: "warning"});
										}else{
											$.ligerDialog.SQalert({content: "保存失败，请稍后再试！", SQ_type: "warning"});
										}
									},
									error:function(e){
										$.ligerDialog.SQalert({content: "网络问题，请稍后重试！", SQ_type: "warning"});
									}
								});
								$('#usrNmSearch').val("");
							}
						}else{
							$('#usrNmSearch').val("");
						}
					});
	    		}
			});
		}
		
		//显示已上传但是未保存的文件列表
		function showFiles(data, index){
			var sysDate = $("#sysDate").val();
			var loginUsrNm = $("#loginUsrNm").val();
			var f_Grid = liger.get("ligergrid_file"+index);
			var uploadJOSN = data;
    		for (var i=0; i< uploadJOSN.length; i++){ 
        		//添加行-追加行
        		f_Grid.addRow({fileNm: uploadJOSN[i].fileNm, crtUsrRlNm: loginUsrNm, crtTm: sysDate, doSthFlag: "1", ligerIndex: index});
    		}
		}
		
		//保存文件
		function saveFile(){
			var docArray = JSON.stringify(getUploadResultParams(1));
	        var url = "<%=path%>/ass/assParking/uploadAttchment.do";
	        var validId = $("#validId").val();
        	if(null == docArray){
        		//$.ligerDialog.SQalert({content: "请选择要上传的文件！",SQ_type: "error"});
        		return false;
        	}
	        if(validId){//有效期ID不为空执行
		        //赋值附件
		        var data = {
					"docArray":docArray,
					"validId":validId
				};
				//请求
		        AjaxUtils.ajaxPost(url, data, function(msg){
		        	if(msg.success == 1){
						 //$.ligerDialog.SQalert({content: msg.info,SQ_type: "success"});
						 if(msg.data){
							 //刷新附件页面GRID方法和清空上传按钮
							 //refreshUlFileRecListByPkId(msg.data,"ASS_PARKING_VALID");
							 clearUploadResultParams(1);
						 }
					 }else{
						 UIUtils.alertError(msg.info);
					 }
		        }, true);
	        }else{
	        	$.ligerDialog.SQalert({content: "业务主键为空-不能保存文件！",SQ_type: "error"});
	        }
		}
		
	</script>
</body>
</html>